package com.ruoyi.system.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.SysRegion;
import com.ruoyi.system.domain.from.SysRegionFrom;
import com.ruoyi.system.domain.vo.SysRegionVo;
import com.ruoyi.system.mapper.SysRegionMapper;
import com.ruoyi.system.service.ISysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 行政划分
 * @Author Nuanyang
 * @Data 2023/5/27 15:13
 */
@Service
public class SysRegionServiceImpl  extends ServiceImpl<SysRegionMapper, SysRegion> implements ISysRegionService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysRegionMapper dao;


    @Override
    public List<SysRegionVo> queryList(SysRegionFrom bo) {
        String key = "sys-region";
        List<SysRegionVo> list = redisCache.getCacheList(key);
        if (!list.isEmpty()) {
            return list;
        }

        List<SysRegion> sysRegions = dao.selectList(new QueryWrapper<SysRegion>());
        list = sysRegions.stream()
                .map(sysRegion -> {
                    SysRegionVo sysRegionVo = new SysRegionVo();
                    BeanUtil.copyProperties(sysRegion, sysRegionVo);
                    return sysRegionVo;
                })
                .collect(Collectors.toList());

        redisCache.setCacheList(key, list);

        return list;
    }

    @Override
    public List<SysRegion> searchList(Integer integer) {
        return dao.selectList(new QueryWrapper<SysRegion>().eq("level_id",integer));
    }


}
